﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessWork_Person_Unit_Post
    {
        /// <summary>
        /// 根据id获取劳防用品配置与发放标准对象
        /// </summary>
        /// <returns></returns>
        public DataSet GetWork_PostById(int Id)
        {
            sqldb db = new sqldb();
            string query = "select [ID] ,[PersonNo],[PersonName],[ArticleId],[RuleId]" +
                            "from Work_Person_Unit_Post where 1=1 and Id = " + Id;
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }
        

        /// <summary>
        /// 根据条件获取所有劳防用品配置与发放标准。
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllWork_Post(string condition)
        {
            sqldb db = new sqldb();
            string query = "select [ID] ,[PersonNo],[PersonName],[ArticleId],[RuleId]" +
            "from Work_Person_Unit_Post where 1=1 " + condition;
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 获取所有劳防用品配置与发放标准(分页）
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllWork_Post(string Condition, int pagesize, int startindex)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select  B.[ID] ,B.[PersonNo],B.[PersonName],B.[ArticleId],B.[RuleId]" +
                              " from (select ROW_NUMBER() OVER(ORDER BY A.ID DESC) rownumber," +
                              " [ID] ,[PersonNo],[PersonName],[ArticleId],[RuleId]" +
                              " from Work_Person_Unit_Post A where 1=1 " + Condition + ") B " +
                              "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                return db.ExecuteDataset(conn, CommandType.Text, query);
            }
        }

        /// <summary>
        /// 保存或修改劳防用品配置与发放标准
        /// </summary>
        /// <param name="dmpara"></param>
        /// <returns></returns>
        public int SaveWork_Post(object[] ArticleType)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                return db.ExecuteNonQuery(conn, "Proc_Work_Person_Unit_Post", ArticleType);
            }
        }


        /// <summary>
        /// 删除劳防用品配置与发放标准
        /// </summary>
        /// <param name="did"></param>
        /// <returns></returns>
        public int DeleteWork_Post(string id)
        {
            sqldb db = new sqldb();
            string query = "delete from Work_Person_Unit_Post where  id = " + id;
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 根据条件获取所有劳防用品配置与发放标准数量
        /// </summary>
        /// <returns></returns>
        public int GetWork_PostCount(string Condition)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Work_Person_Unit_Post where 1=1 " + Condition;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }
    }
}
